package org.example.petsystem.controller;

import org.example.petsystem.config.CodeGenerator;
import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.entity.ShopMessage;
import org.example.petsystem.service.RelProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/ReleseProduction")
@CrossOrigin(origins="*")
public class RelProController {
    @Autowired
    private RelProService relProService;
//卖家添加商品
     @PostMapping("addProduct")
    public ResultBody addProduct(@RequestBody Map<String, Object> map) {
        ShopMessage pet = new ShopMessage();
        pet.setCode(UUID.randomUUID().toString());
        pet.setCategory((String) map.get("category"));
        pet.setName((String) map.get("name"));
        pet.setPrice(Double.valueOf(map.get("price").toString()));
        pet.setDescription((String) map.get("description"));
        pet.setPhone((String) map.get("phone"));
        pet.setAvatarUrl((String) map.get("avatar_Url")); // 前端传avatar_Url，后端转avatarUrl
        pet.setGender((String) map.get("gender"));
        pet.setAge(map.get("age") == null ? null : Integer.valueOf(map.get("age").toString()));
        pet.setCertificate_url((String) map.get("certificate"));

        int result = relProService.addPet(pet);
        if (result > 0) {
            return new ResultBody(200, "添加成功", pet);
        } else {
            return new ResultBody(400, "添加失败", null);
        }
    }
    //显示商品信息（卖家端）
    @RequestMapping("/displayProduct")
    public ResultBody displayProduct(@RequestBody Map<String, Object> map) {
        //判断参数是否完整
        if (map.get("phone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String phone = (String) map.get("phone");
        //判断电话号码是否正确
        if(relProService.displayProduct(phone)==null)
        {
            return new ResultBody(400,"电话号码错误",false);
        }
        return relProService.displayProduct(phone);
    }

    //显示待上架商品信息(管理员端）（废弃）
    @RequestMapping("/displayShopW")
    public ResultBody displayShop() {
        return relProService.getAllMessage();

    }//（71-76废弃）
    //显示已上架商品信息（管理员端）
    @PostMapping("/displayShopR")
    public ResultBody getPetsByPhone() {
        List<ShopMessage> pets = relProService.getPetsByPhone();
//        for (ShopMessage pet : pets) {
//            System.out.println("单个商品信息：" + pet);
//            System.out.println("certificate_url值：" + pet.getCertificate_url());
//        }
        return new ResultBody(200, "查询成功", pets);
    }
    //决策商品是否上架（废弃82-123）
    @RequestMapping("/decisionShop")
    public ResultBody decision(@RequestBody Map<String, Object> map) {
        System.out.println(map);
        if(map.get("decision")==null||map.get("code")==null)
        {//传入决策,商品编号
            return new ResultBody(404,"缺少参数",false);
        }
        Object object = map.get("decision");
        String decision;
        Object object1 = map.get("code");
        String code;
        if(object instanceof Integer)
        {
            decision=((Integer) object).toString();
        }
        else if(object instanceof Double)
        {
            decision=((Double) object).toString();
        }
        else if (object instanceof String)
        {
            decision=(String) object;
        }
        else
            return new ResultBody(404,"缺少参数",false);
        if(object1 instanceof Integer)
        {
            code=((Integer) object1).toString();
        }
        else if(object1 instanceof Double)
        {
            code=((Double) object1).toString();
        }
        else if (object1 instanceof String)
        {
            code=(String) object1;
        }
        else
            return new ResultBody(404,"缺少参数",false);
        return relProService.decision(decision,code);
    }
}   